//
//  WSRegisterView.m
//  FamilyLinkPro
//
//  Created by 江鹏 on 2023/9/5.
//  Copyright © 2023 SiterWell.Co.,Ltd. All rights reserved.
//

#import "WSRegisterView.h"

@implementation WSRegisterView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

// MARK: 添加子控件
- (void)setupSubviews {
    _isValidateEmailOK = NO;
    UILabel *registerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    registerLabel.text = @"注册";
    [self addSubview:registerLabel];
    registerLabel.textColor = UIColorFromRGB_0x(0x000000);
    registerLabel.font = fontBold(24);
    [registerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(20);
        make.top.mas_equalTo(self).offset(NaviHeight + 22);
    }];
    
    UITextField *emailTextField = [[UITextField alloc] initWithFrame:CGRectZero];
    [self addSubview:emailTextField];
    emailTextField.backgroundColor = UIColorFromRGB_0x(0xF2F5F8);
    [emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(40);
        make.right.mas_equalTo(self).offset(-40);
        make.height.mas_equalTo(60);
        make.top.mas_equalTo(registerLabel.mas_bottom).offset(64).priorityLow();
    }];
    emailTextField.borderStyle = UITextBorderStyleNone;
    emailTextField.textAlignment = NSTextAlignmentNatural;
    emailTextField.textColor = BlackColor;
    emailTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    emailTextField.leftViewMode = UITextFieldViewModeAlways;
    [emailTextField addTarget:self action:@selector(emailTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
    
    ViewRadius(emailTextField, 10);
    emailTextField.attributedPlaceholder = [emailTextField placeHolderAttributedString:@"邮箱地址:" color:UIColorFromRGB_0x(0x666666) fontSize:16];
    emailTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    _agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_agreeButton];
    [_agreeButton setBackgroundImage:ImageNamed(@"unselected_48") forState:UIControlStateNormal];
    [_agreeButton setBackgroundImage:ImageNamed(@"selected_48") forState:UIControlStateSelected];
    [_agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(emailTextField);
        make.top.mas_equalTo(emailTextField.mas_bottom).offset(33);
    }];
    [_agreeButton addTarget:self action:@selector(agreeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *agreeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    agreeLabel.text = @"我同意";
    agreeLabel.textColor = UIColorFromRGB_0x(0x666666);
    agreeLabel.font = font(13);
    [self addSubview:agreeLabel];
    [agreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_agreeButton.mas_right).offset(15);
        make.centerY.mas_equalTo(_agreeButton);
    }];
    
    UILabel *userProtocolLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    userProtocolLabel.textColor = UIColorFromRGB_0x(0x4C80F8);
    userProtocolLabel.text = @"用户协议";
    userProtocolLabel.font = font(13);
    [self addSubview:userProtocolLabel];
    userProtocolLabel.userInteractionEnabled = YES;
    [userProtocolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(agreeLabel);
        make.left.mas_equalTo(agreeLabel.mas_right).offset(0);
    }];
    
    UITapGestureRecognizer *userProtocolRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userProtocolClick)];
    [userProtocolLabel addGestureRecognizer:userProtocolRecognizer];
    
    
    UILabel *andLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:andLabel];
    andLabel.text = @"和";
    andLabel.font = font(13);
    andLabel.textColor = UIColorFromRGB_0x(0x666666);
    [andLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(agreeLabel);
        make.left.mas_equalTo(userProtocolLabel.mas_right).offset(0);
    }];
    
    UILabel *privacyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:privacyLabel];
    privacyLabel.text = @"隐私政策";
    privacyLabel.font = font(13);
    privacyLabel.textColor = UIColorFromRGB_0x(0x4C80F8);
    privacyLabel.userInteractionEnabled = YES;
    [privacyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(agreeLabel);
        make.left.mas_equalTo(andLabel.mas_right).offset(0);
    }];
    UITapGestureRecognizer *privacyRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(privacyClick)];
    [privacyLabel addGestureRecognizer:privacyRecognizer];
    
    _obtainYzmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_obtainYzmButton];
    [_obtainYzmButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _obtainYzmButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 0.6);
    _obtainYzmButton.titleLabel.font = fontBold(16);
    ViewRadius(_obtainYzmButton, 20);
    [_obtainYzmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(40);
        make.right.mas_equalTo(self).offset(-40);
        make.top.mas_equalTo(privacyLabel.mas_bottom).offset(30);
        make.height.mas_equalTo(45);
    }];
    _obtainYzmButton.userInteractionEnabled = YES;
    [_obtainYzmButton addTarget:self action:@selector(obtainYzmButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
}


// MARK: Action

#pragma mark -- 用户点击按钮
- (void)agreeButtonClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.isSelected && _isValidateEmailOK) {
        _obtainYzmButton.userInteractionEnabled = YES;
        _obtainYzmButton.backgroundColor = UIColorFromRGB_0x(0x4C80F8);
        return;
    }
    _obtainYzmButton.userInteractionEnabled = NO;
    _obtainYzmButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 0.6);
    
}

#pragma mark -- 用户点击协议
- (void)userProtocolClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(userProtocolClick)]) {
        [self.delegate userProtocolClick];
    }
}

#pragma mark -- 用户点击隐私
- (void)privacyClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(privacyClick)]) {
        [self.delegate privacyClick];
    }
}

#pragma mark -- 用户点获取验证码
- (void)obtainYzmButtonClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(obtainYzmButtonClick)]) {
        [self.delegate obtainYzmButtonClick];
    }
}


#pragma mark -- 监听文本框的变化
- (void)emailTextFieldChange:(UITextField *)sender {
//    NSString *emailStr = sender.text;
//    _isValidateEmailOK = [NSString isValidateEmail:emailStr];
//    if (sender.text.length == 0 || ![NSString isValidateEmail:emailStr] || !_agreeButton.isSelected) {
//        _obtainYzmButton.userInteractionEnabled = NO;
//        _obtainYzmButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 0.6);
//        return;
//    }
//    _obtainYzmButton.userInteractionEnabled = YES;
//    _obtainYzmButton.backgroundColor = UIColorFromRGB_0x(0x4C80F8);
}


@end
